package DP;

/**
 * @author 曹见朋
 * @create 2024-06-16-21:44
 */
public class Main1143 {
    // TODO 1143. 最长公共子序列
    public static int longestCommonSubsequence(String text1, String text2) {
        int text1_len = text1.length();
        int text2_len = text2.length();

        int[][] dp = new int[text1_len + 1][text2_len + 1];
        for(int i = 0; i < text1_len; i++){
            for(int j = 0; j < text2_len; j++){
                dp[i][j] = 0;
            }
        }

        for(int i = 1; i <= text1_len; i++){
            for(int j = 1; j <= text2_len; j++){
                if(text1.charAt(i - 1) == text2.charAt(j - 1)){
                    dp[i][j] = dp[i - 1][j - 1] + 1;
                }else{
                    dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        return dp[text1_len][text2_len];
    }

    public static void main(String[] args) {
        String text1 = "abcde";
        String text2 = "ace";
        System.out.println(longestCommonSubsequence(text1, text2));
    }
}
